package com.resourcestore.action;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.List;

import com.resourcestore.utils.CommonUtil;
import com.resourcestore.utils.tag.Pagination;
import com.resourcestore.action.BaseAction;
import com.resourcestore.dao.OperationsDao;
import com.resourcestore.model.MajorGroup;
import com.resourcestore.model.Operations;

public class OperationsAction extends BaseAction {
	
	private Operations operations;
	private List<Operations> listOperations;
	private OperationsDao operationsDao = new OperationsDao();
	private Pagination pagination = new Pagination();

	
	/**
	 * 查询返回List带分页
	 * @throws ParseException 
	 * 
	 */
	public String listOperations(){
		operations = new Operations();
		pagination.setTotal(operationsDao.queryOperationsNum(operations));
		listOperations = operationsDao.selectOperationsPage(operations,pagination);
		
		for (int i = 0; i < listOperations.size(); i++) {
			try {
				listOperations.get(i).setGmtCreatedString(CommonUtil.getSysTimeFormatString(listOperations.get(i).getGmtCreated()));
				listOperations.get(i).setGmtModifiedString(CommonUtil.getSysTimeFormatString(listOperations.get(i).getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return "default";
	}
	
	/**
	 * 根据条件搜索，查询返回List带分页
	 * @throws ParseException 
	 * 
	 */
	public String searchListOperations(){

		//对内容进行urldecoder解码
		try {
			operations.setOperName(new String(operations.getOperName().getBytes("iso-8859-1"),"UTF-8"));
			operations.setOperUrl(new String(operations.getOperUrl().getBytes("iso-8859-1"),"UTF-8"));
			operations.setRemark(new String(operations.getRemark().getBytes("iso-8859-1"),"UTF-8"));
			operations.setAuthor(new String(operations.getAuthor().getBytes("iso-8859-1"),"UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		pagination.setTotal(operationsDao.queryOperationsNum(operations));
		listOperations = operationsDao.selectOperationsPage(operations,pagination);
		
		for (int i = 0; i < listOperations.size(); i++) {
			try {
				listOperations.get(i).setGmtCreatedString(CommonUtil.getSysTimeFormatString(listOperations.get(i).getGmtCreated()));
				listOperations.get(i).setGmtModifiedString(CommonUtil.getSysTimeFormatString(listOperations.get(i).getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return newpath("/Operations/listOperations.jsp");
	}
	
	/**
	 * 预插入数据
	 * 
	 */
	public String preInsertOperations(){
		
		//operations = operationsDao.selectOperationsById(operations);
		
		return newpath("/Operations/insertOperations.jsp");
	}
	/**
	 * 插入一条数据
	  */		
	public String insertOperations(){
		
		try {
			operations.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			operations.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		operationsDao.insertOperations(operations);
		return redirect(this.getUrl());		
	}
	
	/**
	 * 更新之前加载原信息
	 * 
	 */
	public String preUpdateOperations(){
		operations = operationsDao.selectOperationsById(operations);
		
		return newpath("/Operations/updateOperations.jsp");
	}
	/**
	 * 更新信息
	 * 
	 */
	public String updateOperations(){
		
		try {
			//operations.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			operations.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		operationsDao.updateOperations(operations);
		return redirect(this.getUrl());	
	}

	/**
	 * 删除信息
	 */
	public String deleteOperations(){
		operationsDao.deleteOperations(operations);
		return redirect(this.getUrl());	
	}

	/**
	 * 批量删除信息
	 */
	public String deleteMoreOperations(){
	
		operationsDao.deleteMoreOperations(operations);
		return redirect(this.getUrl());
	}
	
	
	public Operations getOperations() {
		return operations;
	}

	public void setOperations(Operations operations) {
		this.operations = operations;
	}

	public List<Operations> getListOperations() {
		return listOperations;
	}

	public void setListOperations(List<Operations> listOperations) {
		this.listOperations = listOperations;
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	
	
	
	

}
